package project.active;

import model.pojo.Employee;
import project.dao.pr_projectDAO;
import project.model.PrProject;
import project.model.PrTestProject;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TabHost;
import android.widget.TextView;
import approve.action.LoginActivity;
import approve.action.R;

public class Projectdetail_tab_tc extends TabActivity {

	PrProject pro=new PrProject();
	PrTestProject err=new PrTestProject();
	pr_projectDAO pr=new pr_projectDAO();
	Employee emp=new Employee();
	TextView logout,username,report;
	Bundle language;
	
	String lang = "EN",database="admin";
	public String getLang() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			lang = extras.getString("lang");
		}
		return lang;
	}
	
	public String getDatabase() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			database = extras.getString("database");
		}
		return database;
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.projectdetail_tab_tc);
		
		language=getIntent().getExtras().getBundle(getLang());
		
		Intent i=this.getIntent();
		pro=(PrProject)i.getSerializableExtra("project");
		err=(PrTestProject)i.getSerializableExtra("errorproject");
		emp=(Employee)i.getSerializableExtra("em");
		
		Resources res=getResources();
		TabHost tabHost=getTabHost();
		TabHost.TabSpec spec;
		Intent intent;
		

		intent=new Intent().setClass(Projectdetail_tab_tc.this, Projectdetail_listerror_tc.class);
		spec=tabHost.newTabSpec("listerror_tc").setIndicator(language.getString("l_errinfo"),res.getDrawable(R.drawable.ic_tab_artists)).setContent(intent);
		loadIntent(intent);
		tabHost.addTab(spec);
		
		intent=new Intent().setClass(Projectdetail_tab_tc.this, Projectdetail_info_tc.class);
		spec=tabHost.newTabSpec("info_member_tc").setIndicator(language.getString("project_info"), res.getDrawable(R.drawable.ic_tab_artists)).setContent(intent);
		loadIntent(intent);
		tabHost.addTab(spec);
		
		if(getPositionEmployee()==0){
			intent=new Intent().setClass(Projectdetail_tab_tc.this, Projectdetail_addnewerror_tc.class);
			spec=tabHost.newTabSpec("info_member_tc").setIndicator(language.getString("add_error"), res.getDrawable(R.drawable.ic_tab_artists)).setContent(intent);
			loadIntent(intent);
			tabHost.addTab(spec);
		}

		logout=(TextView)findViewById(R.id.logout);
		logout.setText(language.getString("Logout"));
		username=(TextView)findViewById(R.id.username);
		
		logout.setOnClickListener(listenerLogout);
		username.setText(emp.getUserName());
//		report=(TextView)findViewById(R.id.report);report.setText("20");
	}
	
	private void loadIntent(Intent intent){
		intent.putExtras(getIntent().getExtras());
		intent.putExtra("project", pro);
		intent.putExtra("em", emp);
		intent.putExtra("errorproject", err);
		intent.putExtra("database", getDatabase());
		intent.putExtra("lang", lang);
	}
	
	private OnClickListener listenerLogout=new OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent i=new Intent(Projectdetail_tab_tc.this, LogoutActivity.class);
			i.putExtra("database", getDatabase());
			startActivity(i);
		}
	};
	
	private int getPositionEmployee(){
		int result=1;
		result=pr.getPositionEmployee(getDatabase(),emp.getIdemployee(),pro.getIdProject());
		return result;
	}
}
